package com.cwj.blog.app

import android.content.Context
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.tencent.mmkv.MMKV
import com.tencent.upgrade.bean.UpgradeConfig
import com.tencent.upgrade.core.UpgradeManager
import com.yechaoa.yutils.ActivityUtil
import com.yechaoa.yutils.LogUtil
import com.yechaoa.yutils.YUtils


/**
 * Description : App
 */
class App : BaseApplication() {

    override fun onCreate() {
        super.onCreate()
        //初始化
        YUtils.init(this)
        //设置打印开关
        LogUtil.setIsLog(true)
        //注册Activity生命周期
        registerActivityLifecycleCallbacks(ActivityUtil.getActivityLifecycleCallbacks())

        //初始化MMKV
        MMKV.initialize(this)

        //初始化应用升级代码
        val builder = UpgradeConfig.Builder()
        val config = builder.appId("34549a9c84").appKey("7b368754-c3c2-49b8-b1e0-d5acd6f8f76f").build()
        UpgradeManager.getInstance().init(applicationContext, config)
    }

    companion object {
        //static 代码段可以防止内存泄露
        init {
            //设置全局的Header构建器
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context: Context?, layout: RefreshLayout? ->
                ClassicsHeader(context) //.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
            //设置全局的Footer构建器
            SmartRefreshLayout.setDefaultRefreshFooterCreator { context: Context?, layout: RefreshLayout? ->
                ClassicsFooter(
                    context
                ).setDrawableSize(20f)
            }
        }
    }
}